package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberDao memberDao;

  	@Transactional
	@Override
	public void saveOrEdit(String telephone) {
		// 根据手机号获取会员信息
		Member member = memberDao.findByTelephone(telephone);
		if( member == null){
			// 不是会员，自动注册
			member = new Member();
			member.setPhoneNumber(telephone);
			member.setRegTime(new Date());
			// 调用Service保存会员
			memberDao.add(member);
		}
	}

	@Override
	public Map<String, Object> getMemberReport() {
		List<String> months = getMonthOfLastYear();
		List<Long> memberCount = new ArrayList<>();
		//统计每个月份对应的会员数量
		for (String month : months) {
			String firstDay = month +  "-01"; // 2020-01-01
            String lastDay = getThisMonthLastDay(firstDay);
            long count = memberDao.findCountBeforeByDate(lastDay);
			memberCount.add(count);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("months", months);
		map.put("memberCount", memberCount);

		return map;
	}




	private String getThisMonthLastDay(String firstDay){
		//2020-02-01 thisMonthFirstDay
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date firstDate = sdf.parse(firstDay);
			//创建日历对象
			Calendar cal = Calendar.getInstance();
			//设置为本月一号
			cal.setTime(firstDate);
			//调整为下月1号
			cal.add(Calendar.MONTH, 1);
			//调整为前一天(本月的最后一天)
			cal.add(Calendar.DAY_OF_MONTH, -1);

			String str = sdf.format(cal.getTime());
			return str;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取最近一年的月份
	 * @return
	 */
	private List<String> getMonthOfLastYear(){
		List<String> months = new ArrayList<>();
		//获取前12个月份
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -12);
		for (int i = 0; i < 12; i++) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
			String date = simpleDateFormat.format(calendar.getTime());
			months.add(date);
			calendar.add(Calendar.MONTH, 1);
		}
		return months;
	}

	//根据年龄显示
	@Override
	public List<Map<String, Object>> getMumberByAge() {
		return memberDao.getMumberByAge();
	}

	@Override
	public List<Map<String, Object>> getMumberBySex() {
		return memberDao.getMumberBySex();
	}

    @Override
    public Map<String, Object> getReportByDate(String startDate, String endDate) {
		//创建集合
		List<String> monthsList = new ArrayList<>();
		//格式化时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

		try {
			Date date = sdf.parse(startDate);
			String formatdate = sdf.format(date);
			if(!formatdate.equals(endDate)){
				monthsList.add(formatdate);
				date.setMonth(date.getMonth()+1);
			}else {
				monthsList.add(endDate);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<Long> memberCount = new ArrayList<>();
		//统计每个月份对应的会员数量
		for (String month : monthsList) {
			String firstDay = month +  "-01"; // 2020-01-01
			String lastDay = getThisMonthLastDay(firstDay);
			long count = memberDao.findCountBeforeByDate(lastDay);
			memberCount.add(count);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("months", monthsList);
		map.put("memberCount", memberCount);

		return map;
    }

}